//
//  YCNewsView.m
//  LOLBox
//
//  Created by mac on 15-3-12.
//  Copyright (c) 2015年 Honey. All rights reserved.
//

#import "YCNewsView.h"
#import "YCNewSModel.h"
#import "SDImageCache.h"
#import "UIImageView+WebCache.h"

@interface YCNewsView ()
{
    UILabel *_titleLabel;
    UILabel *_descLabel;
    UILabel *_posttimeLabel;
    UIImageView *_imgView;
}

@end

@implementation YCNewsView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 70, 70)];
        _imgView.contentMode = UIViewContentModeScaleToFill;
        _imgView.clipsToBounds = YES;
        
        //加边框
        CALayer *layer = [_imgView layer];
        layer.borderColor = [[UIColor grayColor] CGColor];
        layer.borderWidth = 1.2f;

        
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 5, self.frame.size.width - 85, 40)];
        _descLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, CGRectGetMinY(_titleLabel.frame), self.frame.size.width - 85, 40)];
        _posttimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 70, 150, 30)];
        
        _titleLabel.numberOfLines = 0;
        _titleLabel.font = [UIFont systemFontOfSize:15];
        //_titleLabel.font = [UIFont boldSystemFontOfSize:15];
        _descLabel.numberOfLines = 0;
        _descLabel.font = [UIFont systemFontOfSize:13];
        _descLabel.textColor = [UIColor grayColor];
        _posttimeLabel.font = [UIFont systemFontOfSize:12];
        
        //NSLog(@"%@",_titleLabel.text);
        //分割下划线
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 94, self.frame.size.width, 1)];
        lineView.backgroundColor = [UIColor grayColor];
        lineView.alpha = 0.5;
        
        
        [self addSubview:_imgView];
        [self addSubview:_titleLabel];
        [self addSubview:_descLabel];
        [self addSubview:_posttimeLabel];
        [self addSubview:lineView];
    }
    return self;
}

- (void)fillCellWithModel:(YCNewSModel *)model
{
    //NSLog(@"title11%@",model.title);
    _titleLabel.text = model.title;
    _descLabel.text = model.desc;
    _posttimeLabel.text = [NSString stringWithFormat:@"发表时间:  %@",model.posttime];
    [_imgView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"noimage_xiangqing"]];
    
    // 计算标题高度
    NSDictionary *dict = @{NSFontAttributeName:_titleLabel.font};
    CGRect titleRect = [_titleLabel.text boundingRectWithSize:CGSizeMake(self.frame.size.width - 85, 40) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
    
    //NSLog(@"---%f----%f",titleRect.size.height,titleRect.size.width);
    
    CGRect titleFrame = _titleLabel.frame;
    titleFrame.size.height = CGRectGetHeight(titleRect);
    _titleLabel.frame = titleFrame;
    
    // 计算详情高度
    CGRect descFrame = _descLabel.frame;
    descFrame.origin.y = CGRectGetMaxY(_titleLabel.frame);
    _descLabel.frame = descFrame;

}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
